<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Product extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('Product_model');
    }
	

	public function index()
	{
		$agid = $this->input->get('agid');
		if (!$agid)
			$userid = $this->thatUser['id'];
		else 
			$userid = _get_key_val($this->input->get('agid'),true);
		
		$this->load->model('User_model');
		$user = $this->User_model->get_by_id($userid);
		if (!$user)
		{
			alert('无此用户！','/m/fans');
		}
		$nickname = $user['nickname'];
		//is_post()
		if ($this->input->is_post())
		{
			//验证规则		required必填项
			$config = array(
                array(
                     'field'   => 'code', 
                     'label'   => '工作', 
                     'rules'   => 'required'
                  ),
                array(
                     'field'   => 'price', 
                     'label'   => '工作价格', 
                     'rules'   => 'required'
                  ),
            );
            $arrCode = $this->input->post('code');
			$arrPrice = $this->input->post('price');
			$shoottype = $this->input->post('shoottype');
			$shootpic = $this->input->post('shootpic');
			$swimsuit = $this->input->post('swimsuit');
			$appealsuit = $this->input->post('appealsuit');
			$local_order = $this->input->post('local_order');
			$field_order = $this->input->post('field_order');
			if(count($arrCode)!=count($arrPrice))
			{
				//echo '请正确提交数据';die;
                alert('请正确提交数据！！','/m/product?agid='.$agid);
			}
			//-验证规则
            $this->form_validation->set_rules($config);
            //$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
			if ($this->form_validation->run() === TRUE)
  			{

  				foreach ($arrCode as $key => $v) {
  					
					$aItem = explode('_', $v);
					if(count($aItem)>=3)
					{
						$data = array(					
							'userid'=>$userid,
							'nickname'=>$nickname,					
							'item'=>$aItem[0],					
							'scene'=>$aItem[1],
							'time'=>$aItem[2],
							'price'=>$arrPrice[$key],
						    'shoottype'=>isset($shoottype[$key])?$shoottype[$key]:0,
						    'shootpic'=>isset($shootpic[$key])?$shootpic[$key]:0,
						    'swimsuit'=>isset($swimsuit[$key])?$swimsuit[$key]:0,
						    'appealsuit'=>isset($appealsuit[$key])?$appealsuit[$key]:0,
							'status'=>1,
							'addtime'=>time(),
							'op_userid'=>$this->loginID,
							'op_username'=>$this->loginUserName,
							'op_time'=>time(),
						);
						//保存数据库
		  				$this->Product_model->insert_update($data);
					}	

  				}
  				
  				//更新user_detail表
                $this->load->model('Userdetail_model');
                $this->Userdetail_model->update_by_where(array('userid'=>$userid),array('local_order'=>$local_order,'field_order'=>$field_order));
  				
                alert('保存成功！！', '/m/product?agid='.$agid);
				
  			}//-form_validation TRUE

		}//-is_post()

		$list = $this->Product_model->fetch_rows("userid=$userid and status=1", '*','time,addtime');


		//begin:右侧-推荐用户
		$right_usertype = 1;
		$right_sex = 1;
		if($user['usertype'] == 1)
			$right_usertype = 2;
		if($user['sex']==1)
			$right_sex = 2;
		$arrWhere = array('usertype'=>$right_usertype,'status'=>1,'userlevel'=>1,'sex'=>$right_sex);
		$feild = 'user.id,nickname,userlogo,company';
		$this->load->model('Recommend_model');
		$rightlist = $this->Recommend_model->get_user_list($arrWhere, $feild, 10);
		//end:右侧-推荐用户
		
		//工作价格配置
		$workitem = $this->config->item('workitem');
		$workscene = $this->config->item('workscene');
		$worktime = $this->config->item('worktime');
		$item_label = '工作内容';
		$scene_label = '工作场景';
		
		if ($user['usertype'] == 6)
		{
			$this->load->model('Userdetail_model');
			$userdetail = $this->Userdetail_model->get_info_by_userid($userid);
			if ($userdetail)
			{
				if (empty($userdetail['type']))
				{
					alert('请先选择类型','/m/info');
				}
				$artist_conf = $this->config->item('artist');
				$workitem = $artist_conf[$userdetail['type']]['workitem'];
				$workscene = $artist_conf[$userdetail['type']]['workscene'];
				$worktime = $artist_conf[$userdetail['type']]['worktime'];
				$item_label = $artist_conf[$userdetail['type']]['item_label'];
				$scene_label = $artist_conf[$userdetail['type']]['scene_label'];
			}
		}
		else if (in_array($user['usertype'], array(4,5)))
		{
			$type = $user['usertype']==4?1:2;
			$artist_conf = $this->config->item('artist');
			$workitem = $artist_conf[$type]['workitem'];
			$workscene = $artist_conf[$type]['workscene'];
			$worktime = $artist_conf[$type]['worktime'];
			$item_label = $artist_conf[$type]['item_label'];
			$scene_label = $artist_conf[$type]['scene_label'];
		}

		$result = array(
			'list' => $list,
			'workitem'=> $workitem,
			'workscene'=> $workscene,
			'worktime'=> $worktime,
			'rightlist' => $rightlist,
            'usertype' => $user['usertype'],
			'item_label' => $item_label,
			'scene_label' => $scene_label
			);
		if ($agid)
		{
			$result['oThatID'] = $userid;
			$result['user_info'] = $user;
		}
		//模特增加本地工作时长和外地工作时长
		if ($user['usertype'] == 1)
		{
		    $this->load->model('Userdetail_model');
			$userdetail = $this->Userdetail_model->get_info_by_userid($userid);
			$result['local_order'] = $userdetail['local_order'];
			$result['field_order'] = $userdetail['field_order'];
		}
		$this->load->view('m/product',$result);
	}
        
        /**
         * 删除产品信息
         * 
         */
        public function delProduct()
        {
            $res = array('code'=> 0, 'error_msg'=> '');
            $flag = 0;
            
            $productId = $this->input->post('id');
            if (!empty($productId)) {
                $flag = $this->Product_model->deleteByProId($productId);
                if (!empty($flag)) {
                    $res['code'] = 200;
                } else {
                    $res['code'] = 201;
                    $res['error_msg'] = '删除失败';
                }
            } else {
                $res['code'] = 202;
                $res['error'] = '没有接收到删除的ID';
            }
            
            return $this->view->json($res);
        }

}